package zhihuilan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import zhihuilan.entity.MySms;
import zhihuilan.mapper.MySmsMapper;
import zhihuilan.service.MySmsService;

import java.time.Instant;
import java.util.Date;

@Service
public class MySmsServiceImpl extends ServiceImpl<MySmsMapper, MySms> implements MySmsService {

    @Override
    public MySms selectById(Integer id){
        return baseMapper.selectById(id);
    }

    @Override
    public MySms selectOne(MySms mySms){
        QueryWrapper<MySms> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("telephone",mySms.getTelephone());
        queryWrapper.eq("code",mySms.getCode());
        queryWrapper.eq("status",0);
        queryWrapper.gt("addtime",   Date.from(Instant.now().minusSeconds(600)));
        queryWrapper.orderByDesc("id");
        return  baseMapper.selectOne(queryWrapper);
    }

    @Override
    public Integer updateEntity(MySms mySms){
        QueryWrapper<MySms> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("telephone",mySms.getTelephone());
        queryWrapper.eq("code",mySms.getCode());
        queryWrapper.eq("status",0);
        queryWrapper.gt("addtime",   Date.from(Instant.now().minusSeconds(600)));
        queryWrapper.orderByDesc("id");
        MySms sms = new MySms();
        sms.setVerifytime(new Date());
        sms.setStatus(1);
        return baseMapper.update(sms,queryWrapper);

    }

   @Override
   public Boolean verifysms(MySms mySms){
       MySms sms = selectOne(mySms);
       if(!ObjectUtils.isEmpty(sms)){
           Integer i =  updateEntity(sms);
           if(i>0){
               return  true;
           }
       }
       return false;
   }
}
